//
//  PushManager.m
//  leo
//
//  Created by 常艳 on 15/11/10.
//  Copyright (c) 2015年 mdx. All rights reserved.
//

#import "PushManager.h"
#import "common.h"
#import "PMessage.h"
#import "User.h"
#import "Location.h"
#import "Chat.h"
#import "AppDelegate.h"
#define push_tag @"PushManager-----------------------"
@implementation PushManager
+(PushManager *)sharedSingleton
{
    static PushManager *pushManager;
    @synchronized(self)
    {
        if(pushManager==NULL)
            pushManager=[[PushManager alloc] init];
    }
    return pushManager;
}
-(id)init
{
    self.pushDataManager=[[PushDataManager alloc] init];
    self.dataManager=[[DataManager alloc] init];
    self.syncManager=[[SyncManager alloc] init];
    self.growDataManager=[[GrowDataManager alloc] init];
    self.growManager=[[GrowManager alloc] init];
    return self;
}
-(void)dealloc
{
    self.pushDataManager=NULL;
    self.dataManager=NULL;
    self.syncManager=NULL;
    self.growDataManager=NULL;
    self.growManager=NULL;
}
-(void)explainMessage:(NSDictionary *)message
{
    NSLog(@"%@,explainMessage entered.",push_tag);
    if(message==NULL||[message count]==0)
    {
        NSLog(@"%@,explainMessage,message is null.",push_tag);
        return;
    }
    User *user=[User sharedSingleton];
    if(user==NULL||user.uid==NULL)
    {
        NSLog(@"%@,explainMessage,user is null.",push_tag);
        return;
    }
    int type=[[message valueForKey:@"type"] intValue];
    NSString *uid=[NSString stringWithFormat:@"%@",[message valueForKey:@"uid"]];
    if(uid==NULL||![uid isEqualToString:user.uid])
    {
        NSLog(@"%@,explainMessage,received uncorrect message.",push_tag);
        return;
    }
    NSDictionary *data=[message valueForKey:@"data"];
    if(data==NULL)
    {
        NSLog(@"%@,explianMessage,data is null.",push_tag);
        return;
    }
    NSString *randomId=[message valueForKey:@"rid"];
    switch (type) {
        case PT_VICE_BOUND_REQUEST:
            [self viceBindRequest:data randomId:randomId];
            break;
        case PT_BIND_RELATIONSHIP_CHAGE:
            [self bindRelationChange:data randomId:randomId];
            break;
        case PT_VICE_BIND_REFUSE:
            [self viceBindRefuse:data randomId:randomId];
            break;
        case PT_ADMIN_DISBAND_NOTIFICATION:
            [self adminDisabled:data randomId:randomId];
            break;
        case PT_GEOFENCE_IN_OUT:
            [self geoFenceInOut:data randomId:randomId];
            break;
        case PT_SINGLE_CHAT_UPDATE:
            [self singleChatUpdate:data targetUid:uid];
            break;
        case PT_GROUP_CHAT_UPDATE:
            [self groupChatUpdate:data];
            break;
        case PT_LOCATE_RESULT:
            [self locateResult:data];
            break;
        case PT_SOS:
            [self sos:data];
            break;
        case PT_LOW_BATTERY:
            [self lowBattery:data];
            break;
            /*//不显示在历史通知里
        case PT_BIRTHDAY_NOTIFY:
            [self birthdayNotify:data];
            break;
        case PT_FESTIVAL_NOTIFY:
            [self festivalNotify:data];
            break;*/
        case PT_USER_INFO_UPDATE:
            [self userInfoUpdate:data];
            break;
        case PT_WATCH_SIM_CHANGE:
            [self simChange:data];
            break;
        case PT_TASK:
            [self handleTaskdata:data];
            break;
        case PT_FINISHTASK:
            [self finishTask:data];
            break;
        case PT_USER_LOGIN_ON_OTHER_DEVICE:
            [self userLoginOnOtherDv:data];
            break;
        default:
            break;
    }

}
-(void)viceBindRequest:(NSDictionary *)data randomId:(NSString *)randomId
{
    NSLog(@"%@,viceBindRequest entered.",push_tag);
    if([self checkValid]==NO)
        return;
    if(data==NULL||[data count]==0)
    {
        NSLog(@"%@,viceBindRequest,data is null.",push_tag);
        return;
    }
    User *user=[User sharedSingleton];
    if(user==NULL||user.uid==NULL)
    {
        NSLog(@"%@,viceBindRequest,user is null.",push_tag);
        return;
    }
    //解析数据
    PMessage *message=[[PMessage alloc] init];
    if(message==NULL)
    {
        NSLog(@"%@,viceBindRequest,message is null.",push_tag);
        return;
    }
    message.uid=[data valueForKey:@"uid"];
    message.phoneNum=[data valueForKey:@"tel"];
    message.nickName=[data valueForKey:@"nick"];
    message.randomCode=[data valueForKey:@"rCode"];
    message.alias=[data valueForKey:@"alias"];
    message.deviceUid=[data valueForKey:@"dUid"];
    message.randomId=randomId;
    message.type=PT_VICE_BOUND_REQUEST;
    message.accountId=user.uid;
    //插入数据库
    [self.pushDataManager addBindRequest:message];
    [[NSNotificationCenter defaultCenter] postNotificationName:NEWHISTORYNOTIFICATION object:nil  userInfo:nil];
}
-(void)bindRelationChange:(NSDictionary *)data randomId:(NSString *)randomId

{
    NSLog(@"%@,bindRelationChange entered.",push_tag);
    if([self checkValid]==NO)
        return;
    if(data==NULL||[data count]==0)
    {
        NSLog(@"%@,bindRelationChange,data is null.",push_tag);
        return;
    }
    //解析数据
    /*
    int type=[[data valueForKey:@"type"] intValue];
    if(type==1)//手表有变更，重新获取所有手表
    {
        [self.syncManager getBoundDevices:^(SyncResult result, NSDictionary *retValue, NSString *description) {
            
        }];
    }
    else if(type==2)//管理员有变更
    {
        if(self.accountId==NULL)
            return;
        NSArray *watchLists=[self.dataManager getWatchList:self.accountId];
        if(watchLists==NULL||[watchLists count]==0)
        {
            NSLog(@"%@,bindRelationChange,watchLists is null.",PushDataManager_Tag);
            return ;
        }
        for(int i=0;i<[watchLists count];i++)
        {
            Watch *watch=[watchLists objectAtIndex:i];
            if(watch==NULL||watch.deviceUid==NULL)
                continue;
            [self.syncManager getBoundUsers:watch.deviceUid completion:^(SyncResult result, NSDictionary *retValue, NSString *description) {
                
            }];
        }

    }*/
    [self.syncManager getBoundDevices:^(SyncResult result, NSDictionary *retValue, NSString *description) {
        if(result==SUCESS)
        {
            User *user=[User sharedSingleton];
            if(user==NULL||user.uid==NULL)
                return;
            NSArray *watchLists=[self.dataManager getWatchList:user.uid];
            if(watchLists==NULL||[watchLists count]==0)
            {
                NSLog(@"%@,bindRelationChange,watchLists is null.",PushDataManager_Tag);
                [[NSNotificationCenter defaultCenter] postNotificationName:UNBINDALLDEVICE object:nil  userInfo:nil];                
                return ;
            }
            AppDelegate *mydelegate=[[UIApplication sharedApplication] delegate];
            if(mydelegate!=NULL)
            {
                mydelegate.unbindViceAdminName=NULL;
                mydelegate.isReceiveBindRelationChange=YES;
                mydelegate.bindViceAdminUid=NULL;
            }
            __block int nums=0;
            for(int i=0;i<[watchLists count];i++)
            {
                Watch *watch=[watchLists objectAtIndex:i];
                if(watch==NULL||watch.deviceUid==NULL)
                {
                    nums++;
                    if(nums==[watchLists count])
                        [[NSNotificationCenter defaultCenter] postNotificationName:BINDRELATIONCHANGE object:nil  userInfo:nil];
                    continue;
                }
                [self.syncManager getBoundUsers:watch.deviceUid completion:^(SyncResult result, NSDictionary *retValue, NSString *description) {
                    nums++;
                    if(nums==[watchLists count])
                         [[NSNotificationCenter defaultCenter] postNotificationName:BINDRELATIONCHANGE object:nil  userInfo:nil];
                }];
                /*[self.syncManager getWatchContact:watch.deviceUid completion:^(SyncResult result, NSDictionary *retValue, NSString *description) {
                    
               }];*/
            }

        }
    }];
}

-(void)viceBindRefuse:(NSDictionary *)data randomId:(NSString *)randomId

{
    NSLog(@"%@,viceBindRefuse entered.",push_tag);
    if([self checkValid]==NO)
        return;
    if(data==NULL||[data count]==0)
    {
        NSLog(@"%@,viceBindRefuse,data is null.",push_tag);
        return;
    }
    NSString *deviceUid=[data valueForKey:@"duid"];
    if(deviceUid==NULL||!([deviceUid intValue]>0))
    {
        NSLog(@"%@,viceBindRefuse,deviceUid is null.",push_tag);
        return;
    }
    NSMutableDictionary *deviceUidInfo=[[NSMutableDictionary alloc] init];
    [deviceUidInfo setValue:deviceUid forKey:@"did"];
    [[NSNotificationCenter defaultCenter] postNotificationName:VICEBINDREFUSE object:nil  userInfo:deviceUidInfo];

}
-(void)adminDisabled:(NSDictionary *)data randomId:(NSString *)randomId

{
    NSLog(@"%@,adminDisabled entered.",push_tag);
    if([self checkValid]==NO)
        return;
    if(data==NULL||[data count]==0)
    {
        NSLog(@"%@,adminDisabled,data is null.",push_tag);
        return;
    }
    NSString *deviceUid=[data valueForKey:@"duid"];
    if(deviceUid==NULL||!([deviceUid intValue]>0))
    {
        NSLog(@"%@,adminDisabled,deviceUid is null.",push_tag);
        return;
    }
    NSMutableDictionary *deviceUidInfo=[[NSMutableDictionary alloc] init];
    [deviceUidInfo setValue:deviceUid forKey:@"did"];
    [[NSNotificationCenter defaultCenter] postNotificationName:ADMINDISABLED object:nil  userInfo:deviceUidInfo];
    
}

-(void)geoFenceInOut:(NSDictionary *)data randomId:(NSString *)randomId
{
    NSLog(@"%@,geoFenceInOut entered.",push_tag);
    if([self checkValid]==NO)
        return;
    if(data==NULL||[data count]==0)
    {
        NSLog(@"%@,geoFenceInOut,data is null.",push_tag);
        return;
    }
    User *user=[User sharedSingleton];
    if(user==NULL||user.uid==NULL)
        return;
    PMessage *message=[[PMessage alloc] init];
    if(message==NULL)
    {
        NSLog(@"%@,geoFenceInOut,message is null.",push_tag);
        return;
    }
    message.type=PT_GEOFENCE_IN_OUT;
    message.randomId=randomId;
    message.accountId=user.uid;
    message.deviceUid=[data valueForKey:@"dUid"];
    message.fenceType=[[data valueForKey:@"type"] intValue];
    message.fid=[data valueForKey:@"fid"];
    message.title=[data valueForKey:@"title"];
    message.updateTime=[Tooler getTS];
    NSDictionary *locData=[data valueForKey:@"loc"];
    if(locData!=NULL&&[locData count]>0)
    {
        message.locId=[self addLocationInfo:locData deviceUid:message.deviceUid];
        if(message.locId<0)
            return;
    }
    //更新历史通知数据库
    [self.pushDataManager addFenceInOut:message];
    [[NSNotificationCenter defaultCenter] postNotificationName:NEWHISTORYNOTIFICATION object:nil  userInfo:nil];

}
-(void)singleChatUpdate:(NSDictionary *)data targetUid:(NSString *)targetUid
{
    NSLog(@"%@,singleChatUpdate entered.",push_tag);
    if([self checkValid]==NO)
        return;
    if(data==NULL||[data count]==0||targetUid==NULL)
    {
        NSLog(@"%@,singleChatUpdate,data or targetUid is null.",push_tag);
        return;
    }
    User *user=[User sharedSingleton];
    if(user==NULL||user.uid==NULL)
        return;
    Chat *chat=[[Chat alloc] init];
    chat.user_id=[data valueForKey:@"uid"];
    chat.type=[[data valueForKey:@"type"] intValue];
    chat.sqeId=[data valueForKey:@"sId"];
    chat.pathHash=[data valueForKey:@"hash"];
    chat.duration=[[data valueForKey:@"dur"] intValue];
    chat.genTime=[data valueForKey:@"ts"];
    chat.targetUid=targetUid;
    chat.accountId=user.uid;
    chat.file_url=[data valueForKey:@"url"];
    chat.is_send=1;
    chat.is_read=0;
    chat.is_view=0;
    chat.is_finish=0;
    NSString *seqIdBegin=[self.dataManager getseqIdSingle:chat.user_id];
    BOOL result=[self.dataManager addPushChat:chat];
    if(!result)
        return;
    //下载语音文件
    @try {
        dispatch_queue_t queue=dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
        if(queue==NULL)
        {
            NSLog(@"%@,getAudioByUrl,queue is null",DATAMANAGER_TAG);
            return;
        }
        dispatch_async(queue, ^{
            if(chat.file_url!=NULL&&![chat.file_url isEqualToString:@"(null)"]&&![chat.file_url isEqualToString:@""])
            {
                //[self.syncManager getAudioByUrl:chat.pathHash url:chat.file_url theUid:chat.sqeId type:SINGLEFILE otherId:chat.targetUid];
                [self.syncManager getAudio:chat.pathHash theUid:chat.sqeId type:SINGLEFILE otherId:chat.targetUid];
            }
            else
            {
                [self.syncManager getAudio:chat.pathHash theUid:chat.sqeId type:SINGLEFILE otherId:chat.targetUid];
            }
            if(seqIdBegin!=NULL&&chat.sqeId!=NULL&&[seqIdBegin intValue]+1<[chat.sqeId intValue])
            {
                int limit=[chat.sqeId intValue]-[seqIdBegin intValue];
                [self.syncManager getSingleChatRecords:chat.user_id seqIdBegin:seqIdBegin limit:limit deviceUid:nil completion:^(SyncResult result, NSDictionary *retValue, NSString *description) {
                    
                }];
            }
        });

    }
    @catch (NSException *exception) {
        NSLog(@"%@,singleChatUpdate,exception is %@",DATAMANAGER_TAG,[exception reason]);
    }


  
}
-(void)groupChatUpdate:(NSDictionary *)data
{
    NSLog(@"%@,groupChatUpdate entered.",push_tag);
    if([self checkValid]==NO)
        return;
    if(data==NULL||[data count]==0)
    {
        NSLog(@"%@,groupChatUpdate,data or targetUid is null.",push_tag);
        return;
    }
    User *user=[User sharedSingleton];
    if(user==NULL||user.uid==NULL)
        return;
    Chat *chat=[[Chat alloc] init];
    chat.user_id=[data valueForKey:@"uid"];
    chat.type=[[data valueForKey:@"type"] intValue];
    chat.sqeId=[data valueForKey:@"sId"];
    chat.pathHash=[data valueForKey:@"hash"];
    chat.duration=[[data valueForKey:@"dur"] intValue];
    chat.genTime=[data valueForKey:@"ts"];
    chat.groupId=[[data valueForKey:@"gId"] intValue];
    chat.accountId=user.uid;
    chat.file_url=[data valueForKey:@"url"];
    chat.is_send=1;
    chat.is_read=0;
    chat.is_view=0;
    chat.is_finish=0;
    NSString *seqIdBegin=[self.dataManager getseqId:chat.groupId];
    [self.dataManager addPushChat:chat];
    //下载语音文件
    @try {
        dispatch_queue_t queue=dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
        if(queue==NULL)
        {
            NSLog(@"%@,getAudioByUrl,queue is null",DATAMANAGER_TAG);
            return;
        }
        dispatch_async(queue, ^{
            if(chat.file_url!=NULL&&![chat.file_url isEqualToString:@"(null)"]&&![chat.file_url isEqualToString:@""])
            {
                //[self.syncManager getAudioByUrl:chat.pathHash url:chat.file_url theUid:chat.sqeId type:GROUPCHATFILE otherId:[NSString stringWithFormat:@"%d",chat.groupId]];
                  [self.syncManager getAudio:chat.pathHash theUid:chat.sqeId type:GROUPCHATFILE otherId:[NSString stringWithFormat:@"%d",chat.groupId]];
            }
            else
            {
                [self.syncManager getAudio:chat.pathHash theUid:chat.sqeId type:GROUPCHATFILE otherId:[NSString stringWithFormat:@"%d",chat.groupId]];
            }
            if(seqIdBegin!=NULL&&chat.sqeId!=NULL&&[seqIdBegin intValue]+1<[chat.sqeId intValue])
            {
                int limit=[chat.sqeId intValue]-[seqIdBegin intValue];
                [self.syncManager getGroupChatRecords:chat.groupId seqIdBegin:seqIdBegin limit:limit deviceUid:nil completion:^(SyncResult result, NSDictionary *retValue, NSString *description) {
                    
                }];
            }

        });

    }
    @catch (NSException *exception) {
        NSLog(@"%@,groupChatUpdate,exception is %@",DATAMANAGER_TAG,[exception reason]);
    }
}
-(void)locateResult:(NSDictionary *)data
{
    NSLog(@"%@,locateResult entered.",push_tag);
    if([self checkValid]==NO)
        return;
    if(data==NULL||[data count]==0)
    {
        NSLog(@"%@,locateResult,data  is null.",push_tag);
        return;
    }
    NSDictionary *locData=[data valueForKey:@"loc"];
    NSString *deviceUid=[data valueForKey:@"uid"];
    if(locData==NULL||[locData count]==0||deviceUid==NULL||!([deviceUid intValue]>0))
    {
        NSLog(@"%@,locateResult,locData is null.",push_tag);
        return;
    }
    int ret=[self addLocationInfo:locData deviceUid:deviceUid];
    if(ret<0)
        return;
    [[NSNotificationCenter defaultCenter] postNotificationName:LOCATIONRESULTARRIVED object:nil  userInfo:nil];
}
-(void)sos:(NSDictionary *)data
{
    NSLog(@"%@,sos entered.",push_tag);
    if([self checkValid]==NO)
        return;
    if(data==NULL||[data count]==0)
    {
        NSLog(@"%@,sos,data  is null.",push_tag);
        return;
    }
    User *user=[User sharedSingleton];
    if(user==NULL||user.uid==NULL)
        return;
    PMessage *message=[[PMessage alloc] init];
    if(message==NULL)
    {
        NSLog(@"%@,sos,message is null.",push_tag);
        return;
    }
    message.deviceUid=[data valueForKey:@"dUid"];
    message.accountId=user.uid;
    message.type=PT_SOS;
    NSDictionary *locData=[data valueForKey:@"loc"];
    if(locData!=NULL&&[locData count]>0)
    {
        message.locId=[self addLocationInfo:locData deviceUid:message.deviceUid];
        if(message.locId<0)
            return;
    }
    [self.pushDataManager addSos:message];
    [[NSNotificationCenter defaultCenter] postNotificationName:NEWHISTORYNOTIFICATION object:nil  userInfo:nil];

}
-(void)lowBattery:(NSDictionary *)data
{
    NSLog(@"%@,lowBattery entered.",push_tag);
    if([self checkValid]==NO)
        return;
    if(data==NULL||[data count]==0)
    {
        NSLog(@"%@,lowBattery,data  is null.",push_tag);
        return;
    }
    User *user=[User sharedSingleton];
    if(user==NULL||user.uid==NULL)
        return;
    PMessage *message=[[PMessage alloc] init];
    if(message==NULL)
    {
        NSLog(@"%@,lowBattery,message is null.",push_tag);
        return;
    }
    message.deviceUid=[data valueForKey:@"dUid"];
    message.battery=[[data valueForKey:@"bat"] intValue];
    message.accountId=user.uid;
    message.type=PT_LOW_BATTERY;
    NSDictionary *locData=[data valueForKey:@"loc"];
    if(locData!=NULL&&[locData count]>0)
    {
        message.locId=[self addLocationInfo:locData deviceUid:message.deviceUid];
        if(message.locId<=0)
            return;
    }
    [self.pushDataManager addLowBattery:message];
    [[NSNotificationCenter defaultCenter] postNotificationName:NEWHISTORYNOTIFICATION object:nil  userInfo:nil];
    
}
-(void)birthdayNotify:(NSDictionary *)data
{
    NSLog(@"%@,birthdayNotify entered.",push_tag);
    if([self checkValid]==NO)
        return;
    if(data==NULL||[data count]==0)
    {
        NSLog(@"%@,birthdayNotify,data  is null.",push_tag);
        return;
    }
    User *user=[User sharedSingleton];
    if(user==NULL||user.uid==NULL)
        return;
    PMessage *message=[[PMessage alloc] init];
    if(message==NULL)
    {
        NSLog(@"%@,birthdayNotify,message is null.",push_tag);
        return;
    }
    message.uid=[data valueForKey:@"uid"];
    message.specialTime=[data valueForKey:@"birthday"];
    message.type=PT_BIRTHDAY_NOTIFY;
    message.accountId=user.uid;
    [self.pushDataManager addBirthday:message];
    
}
-(void)festivalNotify:(NSDictionary *)data
{
    NSLog(@"%@,festivalNotify entered.",push_tag);
    if([self checkValid]==NO)
        return;
    if(data==NULL||[data count]==0)
    {
        NSLog(@"%@,festivalNotify,data  is null.",push_tag);
        return;
    }
    User *user=[User sharedSingleton];
    if(user==NULL||user.uid==NULL)
        return;
    PMessage *message=[[PMessage alloc] init];
    if(message==NULL)
    {
        NSLog(@"%@,festivalNotify,message is null.",push_tag);
        return;
    }
    message.specialName=[data valueForKey:@"festerval"];
    message.specialTime=[data valueForKey:@"ts"];
    message.type=PT_FESTIVAL_NOTIFY;
    message.accountId=user.uid;
    [self.pushDataManager addFestival:message];
}
-(void)simChange:(NSDictionary *)data
{
    NSLog(@"%@,simChange entered.",push_tag);
    if([self checkValid]==NO)
        return;
    if(data==NULL||[data count]==0)
    {
        NSLog(@"%@,simChange,data  is null.",push_tag);
        return;
    }
    /*
    PMessage *message=[[PMessage alloc] init];
    if(message==NULL)
    {
        NSLog(@"%@,simChange,message is null.",push_tag);
        return;
    }
    message.type=PT_WATCH_SIM_CHANGE;
    message.deviceUid=[data valueForKey:@"dUid"];
    message.accountId=self.accountId;
    [self.syncManager getBoundDevices:^(SyncResult result, NSDictionary *retValue, NSString *description) {
        
    }];*/
    NSString *deviceUid=[data valueForKey:@"dUid"];
    if(deviceUid==NULL||!([deviceUid intValue]>0))
        return;
    NSMutableDictionary *duidInfo=[[NSMutableDictionary alloc] init];
    [duidInfo setValue:deviceUid forKey:@"dUid"];
    [[NSNotificationCenter defaultCenter] postNotificationName:WATCHSIMCHANGE object:nil  userInfo:duidInfo];
}

-(int)addLocationInfo:(NSDictionary *)locData deviceUid:(NSString *)deviceUid
{
    NSLog(@"%@,addLocationInfo entered.",push_tag);
    if(locData==NULL||[locData count]==0)
    {
        NSLog(@"%@,addLocationInfo,locData is null.",push_tag);
        return 0;
    }
    Location *location=[[Location alloc] init];
    location.deviceUid=deviceUid;
    location.latitude=[[locData valueForKey:@"lat"] floatValue];
    location.longtitude=[[locData valueForKey:@"lgt"] floatValue];
    location.radius=[[locData valueForKey:@"oR"] floatValue];
    location.type=[[locData valueForKey:@"type"] intValue];
    location.locateType=[[locData valueForKey:@"lt"] intValue];
    location.loc=[locData valueForKey:@"pos"];
    location.mainAddress=[locData valueForKey:@"pos"];
    location.gen_time=[locData valueForKey:@"ts"];
    //位置信息插入数据库
    int locId=[self.dataManager addLocation:location];
    NSLog(@"%@,addLocationInfo,locId is %d.",push_tag,locId);
    /*
    if(locId>0)
        return locId;
    else
        return 0;*/
    return locId;
    
}
-(void)userInfoUpdate:(NSDictionary *)data
{
    NSLog(@"%@,userInfoUpdate entered.",push_tag);
    if([self checkValid]==NO)
        return;
    /*
    if(data==NULL)
    {
        NSLog(@"%@,userInfoUpdate,data  is null.",push_tag);
        return;
    }
    int type=[[data valueForKey:@"type"] intValue];
    if(type==1)
    {
        [self.syncManager getBoundDevices:^(SyncResult result, NSDictionary *retValue, NSString *description) {
            
        }];
    }
    else if(type==2)
    {

        if(self.accountId==NULL)
            return;
        NSArray *watchLists=[self.dataManager getWatchList:self.accountId];
        if(watchLists==NULL||[watchLists count]==0)
        {
            NSLog(@"%@,userInfoUpdate,watchLists is null.",PushDataManager_Tag);
            return ;
        }
        for(int i=0;i<[watchLists count];i++)
        {
            Watch *watch=[watchLists objectAtIndex:i];
            if(watch==NULL||watch.deviceUid==NULL)
                continue;
            [self.syncManager getBoundUsers:watch.deviceUid completion:^(SyncResult result, NSDictionary *retValue, NSString *description) {
                
            }];
        }
    }*/
    //dispatch_async(dispatch_get_main_queue(), ^{
        [self.syncManager getBoundDevices:^(SyncResult result, NSDictionary *retValue, NSString *description) {
            //dispatch_async(dispatch_get_main_queue(), ^{
            if(!result==SUCESS)
                return ;
                User *user=[User sharedSingleton];
                if(user==NULL||user.uid==NULL)
                    return;
                NSArray *watchLists=[self.dataManager getWatchList:user.uid];
                if(watchLists==NULL||[watchLists count]==0)
                {
                    NSLog(@"%@,userInfoUpdate,watchLists is null.",PushDataManager_Tag);
                    return ;
                }
                self.watchNum_infoUpdate=0;
                for(int i=0;i<[watchLists count];i++)
                {
                    Watch *watch=[watchLists objectAtIndex:i];
                    if(watch==NULL||watch.deviceUid==NULL)
                    {
                        self.watchNum_infoUpdate++;
                        if(self.watchNum_infoUpdate==[watchLists count])
                            [[NSNotificationCenter defaultCenter] postNotificationName:USERINFOUPDATE object:nil  userInfo:nil];
                        continue;
                    }
                    
                    [self.syncManager getBoundUsers:watch.deviceUid completion:^(SyncResult result, NSDictionary *retValue, NSString *description) {
                        self.watchNum_infoUpdate++;
                        if(self.watchNum_infoUpdate==[watchLists count])
                            [[NSNotificationCenter defaultCenter] postNotificationName:USERINFOUPDATE object:nil  userInfo:nil];
                    }];
                }

            //});
            
        }];

    //});
}
-(void)handleTaskdata:(NSDictionary *)data
{
    if([self checkValid]==NO)
        return;
    if(data==NULL||[data count]==0)
    {
        NSLog(@"%@,handleTaskdata,data is null.",PushDataManager_Tag);
        return;
    }
    Task *task=[[Task alloc] init];
    task.ownerUid=[data valueForKey:@"uid"];
    task.targetUid=[data valueForKey:@"duid"];
    task.type=[[data valueForKey:@"type"] intValue];
    task.action=[[data valueForKey:@"action"] intValue];
    //task.title=[data valueForKey:@"title"];
    //task.desc=[data valueForKey:@"desc"];
    //task.audioHash=[data valueForKey:@"ahash"];
    //task.downAudioUrl=[data valueForKey:@"aurl"];
    //task.photoHash=[data valueForKey:@"phash"];
    //task.downPicUrl=[data valueForKey:@"purl"];
    task.empirical=[[data valueForKey:@"exp"] intValue];
    task.randomCode=[data valueForKey:@"rid"];
    task.expireTs=[[data valueForKey:@"exd"] longValue];
    task.status=UNDO;
    /*
    [self.growDataManager setTask:task];
    
    if(task.downPicUrl!=NULL)
        [self.growManager getTaskPic:task.downPicUrl taskId:task.randomCode type:PUSHTASK];
    if(task.downAudioUrl!=NULL)
        [self.growManager getTaskAudio:task.downAudioUrl taskId:task.randomCode type:PUSHTASK];
    else
    {
        [[NSNotificationCenter defaultCenter] postNotificationName:RECEIVEDPUSHTASK object:nil  userInfo:nil];
    }*/
    [self.growManager getHistoryTask:task.targetUid randomCode:task.randomCode completion:^(SyncResult result, NSDictionary *retValue, NSString *description) {
        
    }];
    
    
}
-(void)finishTask:(NSDictionary *)data
{
    if([self checkValid]==NO)
        return;
    if(data==NULL||[data count]==0)
    {
        NSLog(@"%@,finishTask,data is null.",PushDataManager_Tag);
        return;
    }
    Task *task=[[Task alloc] init];
    task.ownerUid=[data valueForKey:@"uid"];
    task.targetUid=[data valueForKey:@"duid"];
    task.randomCode=[data valueForKey:@"rid"];
    task.audioHash=[data valueForKey:@"ahash"];
    task.downAudioUrl=[data valueForKey:@"aurl"];
    task.duration=[[data valueForKey:@"dur"] integerValue];
    task.status=DONE;
    if(self.growDataManager!=NULL)
        [self.growDataManager confirmTask:task];
    if(task.downAudioUrl!=NULL)
    {
        if(self.growManager==NULL)
            return;
        if(task.downAudioUrl!=NULL&&![task.downAudioUrl isEqualToString:@"(null)"]&&![task.downAudioUrl isEqualToString:@""])
        {
            //[self.growManager getTaskAudio:task.downAudioUrl randomCode:task.randomCode type:PUSHTASK audioHash:task.audioHash theId:task.targetUid];
            [self.growManager getTaskAudioByHash:task.audioHash randomCode:task.randomCode type:PUSHTASK theId:task.targetUid];
        }
        else
        {
            [self.growManager getTaskAudioByHash:task.audioHash randomCode:task.randomCode type:PUSHTASK theId:task.targetUid];
        }
    }
    else
    {
        [[NSNotificationCenter defaultCenter] postNotificationName:RECEIVEDPUSHTASK object:nil  userInfo:nil];
    }
}
-(void)userLoginOnOtherDv:(NSDictionary *)data
{
    if([self checkValid]==NO)
        return;
    [[NSNotificationCenter defaultCenter] postNotificationName:USERALREADYLOGIN object:nil  userInfo:nil];
}

-(BOOL) checkValid
{
    if(self.pushDataManager==NULL||self.dataManager==NULL||self.syncManager==NULL||self.growDataManager==NULL)
    {
        NSLog(@"%@,checkValid,pushDataManager or  dataManager or syncManager or accountId is null",push_tag);
        return NO;
    }
    return YES;
}
@end
